package cn.dj.org.controller;

import cn.dj.basic.annotation.PreAuthorize;
import cn.dj.basic.exception.BusinessException;
import cn.dj.basic.util.ExcelUtils;
import cn.dj.org.domain.ShopAuditLog;
import cn.dj.org.dto.ShopDto;
import cn.dj.org.service.IShopService;
import cn.dj.org.domain.Shop;
import cn.dj.org.query.ShopQuery;
import cn.dj.basic.util.AjaxResult;
import cn.dj.basic.util.PageList;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；店铺
*/
@RestController
@RequestMapping("/shop")
public class ShopController {
    //注入
    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "店铺删除",sn = "shop:delete")//会解析的时候自动根据sn去查数据库表，有数据查到了就修改，没有在数据库表中查到就是添加
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 店铺入驻业务
     * @param shop
     * @return
     */
    @PostMapping(value="/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){//对象接收数据，要求实体类中的字段名与请求参数中的名字一致
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());//e.getMessage()获取抛出的异常信息
        }
    }

    /**
     * 店铺审核通过
     * @param log
     * @return
     */
    @PostMapping(value="/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog log){//对象接收数据，要求实体类中的字段名与请求参数中的名字一致
        try {
            shopService.auditPass(log);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());//e.getMessage()获取抛出的异常信息
        }
    }
    /**
     * 店铺审核失败 - 驳回
     * @param log
     * @return
     */
    @PostMapping(value="/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog log){//对象接收数据，要求实体类中的字段名与请求参数中的名字一致
        try {
            shopService.auditReject(log);
            return new AjaxResult();
        }catch (BusinessException e){//业务异常
            e.printStackTrace();
            //e.getMessage()获取异常信息
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {//系统异常
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");//e.getMessage()获取抛出的异常信息
        }
    }

    /**
     * 数据的导出-导出数据
     */
    @GetMapping(value="/export")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.loadAll();
//        List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response
        ExcelUtils.exportExcel(shops,null,"店铺数据",Shop.class,"系统数据.xlsx",response);
    }

    /**
     * 导入数据
     */
    @PostMapping("/importExcel")//只有一个参数的时候，可以省略value
    public void importExcel(@RequestPart MultipartFile file){
//        MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        //titleRows - 标题的行数-没有标题所以写0，headerRows - 头的行数-有一行所以1。那么就会从第二行开始导出
          //titleRows为1的话 - 有一行，则一共标题一行，头一行。那么会从第三行开始导入数据
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(res->{
            System.out.println(res);
        });
    }

    /**
     * 激活店铺
     * @param id
     * @return
     */
    @GetMapping(value="/active/{id}")
    //@PathVariable获取地址中的请求参数
    public AjaxResult activeId(@PathVariable("id") Long id){//对象接收数据，要求实体类中的字段名与请求参数中的名字一致
        try {
//            System.err.println(id);
            shopService.activeId(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"激活失败");//e.getMessage()获取抛出的异常信息
        }
    }

    /**
     * 店铺报表，查询需要的数据对象进行显示
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopDto> echarts() {
        return shopService.echarts();
    }

}
